package com.choudou5.solr.analyzer.sample;

import com.choudou5.solr.analyzer.PinyinNGramAnalyzer;
import com.choudou5.solr.analyzer.filter.PinyinTransformTokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.StringReader;
import java.util.Date;

/**
 * @name 拼音分词 Demo
 * @author choudou5
 * @date 2018-08-21
 */
public class PinYinAnalyzerDemo {

    private static final IKAnalyzer defaultAnalyzer = new IKAnalyzer(false);
    private static final PinyinNGramAnalyzer pinyinNGramAnalyzer = new PinyinNGramAnalyzer();

    public static void main(String[] args)  throws Exception {
        testPinyin();
    }

    public static void testPinyin() throws Exception {
        //https://www.cnblogs.com/wander1129/p/6658828.html
        System.out.println("testPinyin");
        Date begin = new Date();

        String content = "testGetHotWords 中华人民共和国 公民 日本 人人有责";
        pringString2(content);

        Date end = new Date();
        long SearchTime = end.getTime() - begin.getTime();
        System.out.println("testGetHotWords用时:" + SearchTime / 1000.0D + "(秒)");
    }



    public static void pringString2(String value) throws Exception {
        TokenStream ts = defaultAnalyzer.tokenStream(null, new StringReader(value));
        PinyinTransformTokenFilter tokenFilter = new PinyinTransformTokenFilter(ts);
        tokenFilter.reset();
        CharTermAttribute ta = (CharTermAttribute)tokenFilter.addAttribute(CharTermAttribute.class);
        String chinese = "";
        while (tokenFilter.incrementToken()) {
            chinese = chinese + ta.toString() + " ";
        }
        System.out.println(chinese);
    }

    public static void pringString1(String value) throws Exception {
        TokenStream ts = pinyinNGramAnalyzer.tokenStream(null, new StringReader(value));
        ts.reset();
        CharTermAttribute ta = (CharTermAttribute)ts.addAttribute(CharTermAttribute.class);
        String chinese = "";
        while (ts.incrementToken()){
            System.out.println(ta.toString());
            chinese = chinese + ta.toString() + " ";
        }
        System.out.println(chinese);
    }

}
